import serial as ser

class data_get():
    def data_receive(self, ser, file):
        print("开始接收数据")
        # time_start = time.time()
        # plt.figure()
        # fig = plt.figure()
        # ax1 = plt.axes(projection='3d')

        while True:
            #print("@@@@@@@@@@@@@@@@@@@@@")
            self.out_s = ''
            data = ''
            n = ser.inWaiting()
            if n:
                print(n)

            if n:
                data = ser.read(n)
            n = ser.inWaiting()

            if len(data) == 103 and n == 0:

                try:
                    for i in range(0, len(data)):
                        self.out_s = self.out_s + '{:02X}'.format(data[i]) + ' '
                    #work_time = int(self.out_s[13:15] + self.out_s[10:12] + self.out_s[7:9] + self.out_s[4:6],16) * 5e-3
                    file.writelines(self.out_s)
                    # east_velocity = self.out_s[9:11] + self.out_s[6:8] + self.out_s[3:5] + self.out_s[0:2]
                    # north_velocity = self.out_s[9:11] + self.out_s[6:8] + self.out_s[3:5] + self.out_s[0:2]
                    # up_velocity = self.out_s[9:11] + self.out_s[6:8] + self.out_s[3:5] + self.out_s[0:2]
                    # print(len(data))
                    # gyro_x = 1e-6 * self.get_acc(int(self.out_s[Gyrox_label + 9:Gyrox_label + 11] + self.out_s[Gyrox_label + 6:Gyrox_label + 8] + self.out_s[Gyrox_label + 3:Gyrox_label + 5] + self.out_s[Gyrox_label:Gyrox_label + 2], 16) )
                    # gyro_y = 1e-6 * self.get_acc(int(self.out_s[Gyroy_label + 9:Gyroy_label + 11] + self.out_s[Gyroy_label + 6:Gyroy_label + 8] + self.out_s[Gyroy_label + 3:Gyroy_label + 5] + self.out_s[Gyroy_label:Gyroy_label + 2], 16) )
                    # gyro_z = 1e-6 * self.get_acc(int(self.out_s[Gyroz_label + 9:Gyroz_label + 11] + self.out_s[Gyroz_label + 6:Gyroz_label + 8] + self.out_s[Gyroz_label + 3:Gyroz_label + 5] + self.out_s[Gyroz_label:Gyroz_label + 2], 16) )
                    # acc_x = 1e-6 * self.get_acc(int(self.out_s[Accx_label + 9:Accx_label + 11] + self.out_s[Accx_label + 6:Accx_label + 8] + self.out_s[Accx_label + 3:Accx_label + 5] + self.out_s[Accx_label:Accx_label+2], 16) )
                    # acc_y = 1e-6 * self.get_acc(int(self.out_s[Accy_label + 9:Accy_label + 11] + self.out_s[Accy_label + 6:Accy_label + 8] + self.out_s[Accy_label + 3:Accy_label + 5] + self.out_s[Accy_label:Accy_label+2], 16) )
                    # acc_z = 1e-6 * self.get_acc(int(self.out_s[Accz_label + 9:Accz_label + 11] + self.out_s[Accz_label + 6:Accz_label + 8] + self.out_s[Accz_label + 3:Accz_label + 5] + self.out_s[Accz_label:Accz_label+2], 16) )

                    # x_move_distance = 0.1 * self.get_acc(int(self.out_s[X_mov + 9:X_mov + 11] + self.out_s[X_mov + 6:X_mov + 8] + self.out_s[X_mov + 3:X_mov + 5] + self.out_s[X_mov:X_mov + 2],16))
                    # y_move_distance = 0.1 * self.get_acc(int(self.out_s[Y_mov + 9:Y_mov + 11] + self.out_s[Y_mov + 6:Y_mov + 8] + self.out_s[Y_mov + 3:Y_mov + 5] + self.out_s[Y_mov:Y_mov + 2],16))
                    # h_move_distance = self.get_acc(int(self.out_s[H_mov + 9:H_mov + 11] + self.out_s[H_mov + 6:H_mov + 8] + self.out_s[H_mov + 3:H_mov + 5] + self.out_s[H_mov:H_mov + 2],16))
                    #print(work_time)


                    print("################################")
                    # data = str(work_time) + ' ' + str(x_move_distance) + ' ' + str(y_move_distance) + ' ' + str(h_move_distance) + '\r'
                    # print(data)
                    # file.writelines(data)
                except IndexError:
                    pass



if __name__ == '__main__':
    print('程序开始：')
    save_file = open('./save_data.txt', 'w')
    t = []
    az = []
    ay = []
    ax = []
    gx = []
    gy = []
    gz = []
    xm = []
    ym = []
    hm = []
    ser1 = ser.Serial('/dev/ttyTHS1', baudrate=460800, timeout=0.07)
    # ser1 = serial.Serial('COM10', baudrate=115200, timeout=0.01)
    # ser1.open()
    # data_get().data_send(ser1, data_get().send_static_north())
    # data_get().data_send(ser1,data_get().send_static_north())
    if ser1.isOpen():
        print("串口打开")
        data_get().data_receive(ser1, save_file)
        # data_get().stm32_data_recieve(ser1)
    save_file.close()
    print('程序结束')
